﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace LdCms.Plugins.Map.Tencent.Common
{
    using LdCms.Common.Security;
    using LdCms.Common.Logs;

    public static class Utilities
    {
        public static string GetSign(SortedDictionary<string, string> sParams, string path,string sk)
        {
            try
            {
                int i = 0;
                StringBuilder sb = new StringBuilder();
                foreach (KeyValuePair<string, string> temp in sParams)
                {
                    if (temp.Value == null || temp.Value == "" || temp.Key.ToLower() == "sign")
                        continue;
                    i++;
                    if (i > 1) { sb.Append("&"); }
                    sb.Append(temp.Key.Trim() + "=" + temp.Value.Trim());
                }
                string signKey = string.Format("{0}?{1}{2}", path, sb.ToString(), sk);
                return AlgorithmHelper.MD5(signKey).ToLower();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public static SortedDictionary<string, string> EntityToSortedDictionary<T>(this T obj)
        {
            try
            {
                SortedDictionary<string, string> sParams = new SortedDictionary<string, string>();
                foreach (PropertyInfo property in obj.GetType().GetProperties())
                {
                    string name = property.Name.ToLower();
                    var values = property.GetValue(obj, null);
                    if (!string.IsNullOrWhiteSpace(values == null ? "" : values.ToString()))
                    {
                        sParams.Add(name, values.ToString());
                    }
                }
                return sParams;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public static string SortedDictionaryToUrlParams(SortedDictionary<string, string> sParams)
        {
            int i = 0;
            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair<string, string> temp in sParams)
            {
                if (temp.Value == null)
                    continue;
                i++;
                var strSplit = i == 1 ? "" : "&";
                sb.Append(string.Format("{0}{1}={2}", strSplit, temp.Key.Trim(), temp.Value.ToString().Trim()));
            }
            return sb.ToString();
        }
        public static string MiniClear(this string str)
        {
            try
            {
                string result = str.Replace(" ", "");
                result = result.Replace("\n", "");
                result = result.Replace("\t", "");
                result = result.Replace("\r", "");
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}
